package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.Pharmacy;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.form.PharmacyForm;
import com.lin.server.PharmacyService;
import com.lin.utils.KeyUtils;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MYQ on 2017/10/27.
 */
@Controller
@RequestMapping("/pharmacy")
public class SellerPharmacyController {

    @Autowired
    private PharmacyService service;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("pharmacy/index");
    }

    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        Map<String,Object> map=new HashMap<>();

        Page<Pharmacy> pharmacyPage=service.findAll(name,startTime,endTime,request1);
        map.put("rows",pharmacyPage.getContent());
        map.put("total",service.getSize(name,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) String id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Pharmacy pharmacy=service.findOne(id);
            map.put("pharmacy",pharmacy);
        }
        return new ModelAndView("pharmacy/add_view");
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(@Valid PharmacyForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        Pharmacy pharmacy=new Pharmacy();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                pharmacy=service.findOne(form.getId());
            }
            else {
                form.setId(KeyUtils.getUniqueKey());
            }
            BeanUtils.copyProperties(form,pharmacy);
            System.out.println(pharmacy.toString());
            service.save(pharmacy);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = true) String id){
        Pharmacy pharmacy=service.findOne(id);
        if (pharmacy==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        service.del(pharmacy);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/openLocation")
    public ModelAndView openLocation(HttpServletRequest request){
        String location=request.getParameter("location");
        String address=request.getParameter("address");
        System.out.println(location);
        if (StringUtils.isNotEmpty(location)){
            String[] str=location.split(",");
            request.setAttribute("longitude", str[0]);
            request.setAttribute("latitude", str[1]);
            request.setAttribute("address", address);
        }
        else{
            request.setAttribute("longitude", "108.29723356");
            request.setAttribute("latitude", "22.80649294");
            request.setAttribute("address", "默认位置");
        }
        return new ModelAndView("pharmacy/getLocation");
    }
}
